﻿using MediatR;
using Production_Get.API.Application.Commands.Technologys;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.BasicInformation.Materials;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Get.API.Application.Commands.Procurement
{
    /// <summary>
    /// 获取订单类型数据
    /// </summary>
    public class PurchasingTypeShowHandler : IRequestHandler<PurchasingTypeShowCommand, Result>
    {
        private readonly IBaseRepositoryGet<PurchasingType> purchasingTyperepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<PurchasingTypeShowHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="purchasingTyperepository"></param>
        /// <param name="logger"></param>
        public PurchasingTypeShowHandler(IBaseRepositoryGet<PurchasingType> purchasingTyperepository, ILogger<PurchasingTypeShowHandler> logger)
        {
            this.purchasingTyperepository = purchasingTyperepository;
            this.logger = logger;

        }

        public async Task<Result> Handle(PurchasingTypeShowCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            string sql = "SELECT * FROM PurchasingType";
            try
            {
                logger.LogInformation("获取订单类型数据");
                var list = purchasingTyperepository.GetAll(sql, default).ToList();
                if (list != null)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = list;
                    result.Msg = "显示成功";
                }

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("获取订单类型数据失败", ex.Message);
                throw;
            }
        }
    }
}
